/*
Copyright (c) 2008 Ryan BB Schreiber, Brendan G Fry

Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal in 
the Software without restriction, including without limitation the rights to 
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do 
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Darkside.GeneratorStudio.Library.Domain;
using Darkside.GeneratorStudio.Library.Generator;
using log4net;

namespace Darkside.GeneratorStudio.UI
{
    
    static class Program
    {
        private static readonly ILog _Log = LogManager.GetLogger(typeof(Program));

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            #region Allow for filename to be passed on the command line
            // Yeah, nasty. Will need to be refactored out ASAP. I just really need the app to accept a file name for now...
            // Please don't flame me :)
            var projectFileName = String.Empty;
            if (args.Length > 0)
            {
                projectFileName = args[0];
                if (!File.Exists(projectFileName))
                {
                    //TODO: Extract the parameter parsing elsewhere
                    //TODO: Write to console OR
                    //TODO: Make a proper message with parameters that can be passed
                    MessageBox.Show("Invalid parameters passed or invalid file name", "Generator Studio");
                    return;
                }

                if (args.Length > 1 && args[1] == "generate")
                {
                    GenerateProject(projectFileName);
                    return;
                }
            }
            #endregion Allow for filename to be passed on the command line
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.Run(new MainForm(projectFileName));
        }

        private static void GenerateProject(string projectFileName)
        {
            var project = Project.LoadProject(projectFileName);
            if (project.AllSelected)
            {
                foreach (var table in project.Tables)
                {
                    table.Settings.Active = true;
                }
            }
            var modelGenerator = new ModelGenerator(project);
            modelGenerator.GenerateClasses();
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            _Log.Error(e.Exception.Message, e.Exception);
        }
    }
}
